package com.junyuan.gsp.service;

import com.junyuan.gsp.bean.Product;
import com.junyuan.gsp.bean.Result;
import com.junyuan.gsp.dao.ProductDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: junyuan
 */
@Service
public class ProductService
{
    @Autowired
    private ProductDao productDao;

    public List<Product> findProducts(Map<String, Object> map)
    {
        return productDao.findProducts(map);
    }

    public Integer findProductsCount(Map<String, Object> map)
    {
        return productDao.findProductsCount(map);
    }

    public Result findProductsResult(Map<String, Object> map)
    {
        Integer page = (Integer) map.get("page");
        Integer limit = (Integer) map.get("limit");
        if (page != null && limit != null)
        {
            map.put("start", (page - 1) * limit);
            map.put("length", limit);
        }
        int count = productDao.findProductsCount(map);
        List<Product> data = productDao.findProducts(map);
        return new Result(data, count);
    }

    public Product findProductById(Integer id)
    {
        return productDao.findProductById(id);
    }

    public void saveProduct(Product product)
    {
        if (product.getId() != null && product.getId() != 0)
        {
            productDao.updateProduct(product);
        } else
        {
            productDao.insertProduct(product);
        }
    }

    public void deleteProductById(Integer id)
    {
        productDao.deleteProductById(id);
    }

    public void deleteProducts(String ids)
    {
        productDao.deleteProducts(ids);
    }

}
